package com.hd123.rumba.gwt.widget2e.client.dialog;

import com.google.gwt.user.client.ui.VerticalPanel;
import com.hd123.rumba.gwt.widget2.client.button.RButton;
import com.hd123.rumba.gwt.widget2.client.dialog.RDialog;
import com.hd123.rumba.gwt.widget2.client.dialog.RDialogButtonClickListener;
import com.hd123.rumba.gwt.widget2.client.form.RForm;

/**
 * 筛选对话框
 * 
 * @author LiQi
 * 
 */
public class RFilterDialog extends RDialog {

  /** 基础面板 */
  private VerticalPanel root;

  /** 表单 */
  private RForm form;

  /** 回调 */
  private RFilterCallback filterCallback;

  public RFilterDialog(RFilterCallback filterCallback) {
    super();
    this.filterCallback = filterCallback;

    setCaptionText("筛选");
    setWorkingAreaPadding(6);

    setButtons(new ButtonConfig[] {
        BUTTON_OK, BUTTON_CANCEL, BUTTON_CLEAR });
    addButtonClickListener(new ButtonListener());

    root = new VerticalPanel();
    root.setWidth("100%");
    setWidget(root);

    form = new RForm();
    root.add(form);

    filterCallback.buildConditions(form);
    form.setFieldRequired(false);
    form.focusOnFirstField();
  }

  /**
   * 按钮事件处理
   * 
   * @author LiQi
   * 
   */
  private class ButtonListener implements RDialogButtonClickListener {

    public boolean onClick(RDialog dialog, ButtonConfig buttonConfig, RButton button) {
      if (buttonConfig.equals(BUTTON_OK)) {
        filterCallback.onQuery();
        return true;
      } else if (buttonConfig.equals(BUTTON_CLEAR)) {
        filterCallback.clearConditions();
        return false;
      }
      return true;
    }
  }
}
